home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-12-12 | 27.9 KB | 1,180 lines | [TEXT/CWIE] |
- /******************************************************************************
- ** **
- ** Module: NSTestApp.cp **
- ** **
- ** **
- ** Purpose: Test harness for NetSprocket **
- ** **
- ** Copyright (C) 1996 Apple Computer, Inc. All rights reserved. **
- ** **
- ** **
- *****************************************************************************/
-
- #define VERBOSE 0
-
- #include "NSTestApp.h"
- #include "CPlayerWindow.h"
- #include <LGrowZone.h>
- #include <LWindow.h>
- #include <PP_Messages.h>
- #include <PP_Resources.h>
- #include <PP_KeyCodes.h>
- #include <PPobClasses.h>
- #include <UDrawingState.h>
- #include <UMemoryMgr.h>
- #include <URegistrar.h>
- #include <LEditField.h>
- #include <iostream>
- #include <sioux.h>
- #include <stdio.h>
- #include <UModalDialogs.h>
- #include <LArrayIterator.h>
- #include <LString.h>
- #include <Fonts.h>
- #include <Strings.h>
- #include <OpenTptAppleTalk.h>
-
- #include <NetSprocket.h>
-
- // put declarations for resource ids (ResIDTs) here
-
- const CommandT cmd_Host = 2000;
- const CommandT cmd_Join = 2001;
- const CommandT cmd_Unadvertise = 2002;
- const CommandT cmd_Start = 2003;
- const CommandT cmd_Stop = 2007;
- const CommandT cmd_TestGroups = 2004;
- const CommandT cmd_GetPlayers = 2005;
- const CommandT cmd_GetGroups = 2006;
- const CommandT cmd_GetInfo = 2050;
-
- enum {kKeyboardState = 600, kGameStart, kGameEnd, kTestGroups, kPropogateOptions, kResyncMessage};
- Boolean IsPressed(unsigned short k);
- pascal Boolean MyJoinRequestHandler(NSpGameReference inGame,
- NSpJoinRequestMessage *inMessage, void* inContext,
- Str255 outReason);
-
- const char *kJoinRequestString = "Sprockets";
- //const StringPtr kNBPNameString = "\pNetSprocketTest11";
- const StringPtr kNBPNameString = "\pNSpT";
-
- const UInt32 gPadCode = 0xDEADBEEF;
- UInt32 *oneKMessage;
- UInt32 *tenKMessage;
-
- Boolean gGameOver;
- SInt32 gCountdown;
- NSpFlags flags;
-
- UInt32 gMessagePriority = 1;
- UInt32 gSendRate = 1;
- UInt32 gSendSize = 1;
- Boolean gBlocking = false;
- Boolean gCallWNE = true;
-
- Boolean gServerOnly;
-
- UnsignedWide startTime;
- UnsignedWide baseTime;
- // ===========================================================================
- // • Main Program
- // ===========================================================================
-
- pascal Boolean MyJoinRequestHandler(NSpGameReference inGame,
- NSpJoinRequestMessage *inMessage, void* inContext,
- Str255 outReason)
- {
- LString::CopyPStr("\pYou forgot to say 'Simon says.'", outReason);
-
- return BlocksAreEqual(inMessage->customData, kJoinRequestString,
- inMessage->customDataLen);
- }
-
- void main(void)
- {
- SIOUXSettings.autocloseonquit = true;
- SIOUXSettings.asktosaveonclose = true;
- SIOUXSettings.initializeTB = false;
- SIOUXSettings.setupmenus = false;
- SIOUXSettings.standalone = false;
- // Set Debugging options
- SetDebugThrow_(debugAction_SourceDebugger);
- SetDebugSignal_(debugAction_SourceDebugger);
- InitializeHeap(3); // Initialize Memory Manager
- // Parameter is number of Master Pointer
- // blocks to allocate
-
- // Initialize standard Toolbox managers
- UQDGlobals::InitializeToolbox(&qd);
- gGameOver = false;
- gCountdown = 100;
- flags = kNSpSendFlag_Normal | kNSpSendFlag_SelfSend;
- new LGrowZone(20000); // Install a GrowZone function to catch
- // low memory situations.
- NSTestApp theApp; // replace this with your App type
-
- oneKMessage = (UInt32 *)NewPtr(1024);
- tenKMessage = (UInt32 *)NewPtr((10 * 1024));
-
- for (int i = 12; i < 256; i++)
- {
- oneKMessage[i] = gPadCode;
- }
- for (int i = 12; i < 2560; i++)
- {
- tenKMessage[i] = gPadCode;
- }
- theApp.Run();
- }
-
-
- // ---------------------------------------------------------------------------
- // • CPPStarterApp // replace this with your App type
- // ---------------------------------------------------------------------------
- // Constructor
-
- NSTestApp::NSTestApp()
- {
- // Register functions to create core PowerPlant classes
- bHost = false;
- bAdvertising = false;
- bRunning = false;
- theGame = NULL;
- RegisterAllPPClasses();
- URegistrar::RegisterClass(CPlayerWindow::class_ID, (ClassCreatorFunc) CPlayerWindow::CreatePlayerWindowStream);
- gPulseBit = 0;
-
- // Initialize our player list
- CPlayerListComparator *theComparator = new CPlayerListComparator();
- ThrowIfNULL_(theComparator);
- mPlayerList = new LArray(sizeof(PlayerListItem), theComparator, true);
- ThrowIfNULL_(mPlayerList);
-
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~NSTestApp // replace this with your App type
- // ---------------------------------------------------------------------------
- // Destructor
- //
-
- NSTestApp::~NSTestApp()
- {
- }
-
- // ---------------------------------------------------------------------------
- // • StartUp
- // ---------------------------------------------------------------------------
- // This function lets you do something when the application starts up.
- // For example, you could issue your own new command, or respond to a system
- // oDoc (open document) event.
-
- void
- NSTestApp::StartUp()
- {
- OSStatus err;
- UInt32 choice = 0;
- NumVersion vers;
-
- ObeyCommand(cmd_New, nil); // EXAMPLE, create a new window
-
- // Do some quick and dirty testing
- cout << "Starting up" << endl;
- err = NSpInitialize(500, 50000, 25, 'NSpT', 5000);
- cout << "NSpInitialize: " << err << endl;
- err = NSpInstallJoinRequestHandler(&MyJoinRequestHandler, NULL);
- cout << "NSpInstallJoinRequestHandler returned " << err << endl;
- vers = NSpGetVersion();
- cout << "NetSprocket version is " << *((UInt32 *) &vers) << endl;
- }
-
-
- Boolean IsPressed(unsigned short k)
- // k = any keyboard scan code, 0-127
- {
- KeyMap map;
- unsigned char km[16];
-
- GetKeys(map);
- BlockMove(map, km, 16);
-
- Boolean pressed;
- pressed = ( ( km[k>>3] >> (k & 7) ) & 1);
-
- return pressed;
- }
- int gLastTickCount = 0;
-
- void
- NSTestApp::ProcessNextEvent()
- {
- EventRecord macEvent;
- NSpMessageHeader *theMessage;
- OSStatus status;
-
- if (gCallWNE)
- {
- if (IsOnDuty()) {
-
- // Calling OSEventAvail with a zero event mask will always
- // pass back a null event. However, it fills the EventRecord
- // with the information we need to set the cursor shape--
- // the mouse location in global coordinates and the state
- // of the modifier keys.
-
- ::OSEventAvail(0, &macEvent);
- AdjustCursor(macEvent);
- }
-
- // Retrieve the next event. Context switch could happen here.
-
- Boolean gotEvent = ::WaitNextEvent(everyEvent, &macEvent, 0,
- mMouseRgnH);
-
-
- Boolean SIOUXDidEvent = SIOUXHandleOneEvent(&macEvent);
- // if (!SIOUXDidEvent)
- {
- SetUpdateCommandStatus(false);
-
- // Let Attachments process the event. Continue with normal
- // event dispatching unless suppressed by an Attachment.
-
- if (LAttachable::ExecuteAttachments(msg_Event, &macEvent)) {
- if (gotEvent) {
- DispatchEvent(macEvent);
- } else {
- UseIdleTime(macEvent);
- }
- }
- }
- // Repeaters get time after every event
- LPeriodical::DevoteTimeToRepeaters(macEvent);
-
- // Update status of menu items
- if (IsOnDuty() && GetUpdateCommandStatus()) {
- UpdateMenus();
- }
- }
- else if (IsPressed(vkey_F12))
- gCallWNE = true;
-
- if (theGame && bRunning /*&& IsPressed(vkey_F1)*/)
- {
- size_t messageLen;
- UInt32 interval;
-
- UInt32 count = TickCount();
- if ( count >= gLastTickCount)
- {
- TKeyState state;
-
- switch(gSendRate)
- {
- case 1:
- interval = 60;
- break;
- case 2:
- interval = 6;
- break;
- case 3:
- interval = 2;
- };
-
- gLastTickCount = count + interval;
-
- switch(gSendSize)
- {
- case 1:
- messageLen = sizeof(TKeyState);
- theMessage = (NSpMessageHeader *)&state;
- break;
- case 2:
- messageLen = 1024;
- theMessage = (NSpMessageHeader *) oneKMessage;
- break;
- case 3:
- messageLen = 10 * 1024;
- theMessage = (NSpMessageHeader *) tenKMessage;
- break;
- }
-
- NSpClearMessageHeader(theMessage);
- theMessage->what = kKeyboardState;
- theMessage->to = kNSpAllPlayers;
- theMessage->messageLen = messageLen;
-
- ((TKeyState *)theMessage)->keyMap[0] = IsPressed(0x7e) ? 1 : 0;
- ((TKeyState *)theMessage)->keyMap[1] = IsPressed(0x7d) ? 1 : 0;
- ((TKeyState *)theMessage)->keyMap[2] = IsPressed(0x7c) ? 1 : 0;
- ((TKeyState *)theMessage)->keyMap[3] = IsPressed(0x7b) ? 1 : 0;
- ((TKeyState *)theMessage)->pulseBit = gPulseBit;
- gPulseBit = (gPulseBit == 1) ? 0 : 1;
-
- switch(gMessagePriority)
- {
- case 1:
- flags = kNSpSendFlag_Junk;
- break;
- case 2:
- flags = kNSpSendFlag_Normal;
- break;
- case 3:
- flags = kNSpSendFlag_Registered;
- break;
- }
- flags |= kNSpSendFlag_SelfSend;
- flags |= kNSpSendFlag_FailIfPipeFull;
-
- if (gBlocking)
- flags |= kNSpSendFlag_Blocking;
-
- status = NSpMessage_Send(theGame, theMessage, flags);
- if (status != noErr)
- cout << "NSpMessage_Send returned: " << status << endl;
- }
- }
-
- if (theGame)
- {
- while ((theMessage = NSpMessage_Get(theGame)) != NULL)
- {
- HandleGameEvent(theMessage);
- NSpMessage_Release(theGame, (NSpMessageHeader *)theMessage);
- if (gGameOver)
- {
- status = NSpGame_Dispose(theGame, 0);
- cout << "NSpGame_Dispose returned " << status << endl;
- theGame = NULL;
- bRunning = false;
- break;
- }
- }
- }
-
- }
-
-
- void NSTestApp::HandleGameEvent(NSpMessageHeader *inEvent)
- {
- #if VERBOSE
- cout << "Handling event:";
- cout << "\twhat = " << inEvent->what << endl;
- cout << "\twhen = " << inEvent->when << endl;
- cout << "\tfrom = " << inEvent->from << endl;
- cout << "\tto = " << inEvent->to << endl;
- cout << "\tid = " << inEvent->id << endl;
- cout << "\tmessageLen = " << inEvent->messageLen << endl;
- #endif
- switch (inEvent->what)
- {
- case kNSpJoinApproved:
- break;
- case kNSpJoinDenied:
- break;
- case kNSpPlayerJoined:
- HandlePlayerJoined((NSpPlayerJoinedMessage *)inEvent);
- break;
- case kNSpPlayerLeft:
- HandlePlayerLeft((NSpPlayerLeftMessage *)inEvent);
- break;
- case kKeyboardState:
- HandleKeyboardState((TKeyState *)inEvent);
- break;
- case kGameStart:
- HandleGameStart(inEvent);
- break;
- case kGameEnd:
- HandleGameEnd(inEvent);
- break;
- case kNSpGameTerminated:
- HandleGameTerminated((NSpGameTerminatedMessage *) inEvent);
- break;
- case kTestGroups:
- HandleTestGroups(inEvent);
- break;
- case kPropogateOptions:
- HandlePropogateOptions((OptionsMessage *) inEvent);
- break;
- case kResyncMessage:
- HandleResyncMessage((TKeyState *) inEvent);
- break;
- default:
- cout << "Unhandled game event:" << inEvent->what << endl;
- break;
- }
- }
-
- void NSTestApp::HandlePlayerJoined(NSpPlayerJoinedMessage *inEvent)
- {
- PlayerListItem item;
- NSpMessageHeader theMessage;
- OSStatus status;
-
- cout << "In HandlePlayerJoined" << endl;
-
- if (inEvent->playerInfo.id == NSpPlayer_GetMyID(theGame))
- return;
-
- if (bRunning)
- {
- if (bHost)
- {
- NSpClearMessageHeader(&theMessage);
- theMessage.what = kGameStart;
- theMessage.to = inEvent->playerInfo.id;
- theMessage.messageLen = sizeof(NSpMessageHeader);
-
- status = NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered);
- ThrowIfOSErr_(status);
- }
-
- LWindow *window = LWindow::CreateWindow(1002, this);
- ThrowIfNULL_(window);
- window->SetDescriptor(inEvent->playerInfo.name);
- window->Show();
- item.player = inEvent->playerInfo.id;
- item.window = window;
- mPlayerList->InsertItemsAt(1, 0, &item);
- }
-
- }
-
- void NSTestApp::HandlePlayerLeft(NSpPlayerLeftMessage *inEvent)
- {
- cout << "In HandlePlayerLeft" << endl;
-
- ArrayIndexT index = mPlayerList->FetchIndexOfKey(&inEvent->playerID);
- PlayerListItem item;
- if (mPlayerList->FetchItemAt(index, &item))
- item.window->DoClose();
- mPlayerList->RemoveItemsAt(1, index);
- }
-
- void NSTestApp::HandleGameStart(NSpMessageHeader *inEvent)
- {
- UInt32 i;
- LWindow *window;
- PlayerListItem item;
- LStr255 title;
- OSStatus err;
-
- NSpPlayerEnumerationPtr thePlayers;
- NSpPlayerInfoPtr playerInfo;
-
- err = NSpPlayer_GetEnumeration(theGame, &thePlayers);
- if (err == noErr)
- {
- for (i = 0; i < thePlayers->count; i++)
- {
- playerInfo = thePlayers->playerInfo[i];
- window = LWindow::CreateWindow(1002, this);
- window->SetDescriptor(playerInfo->name);
- window->Show();
- item.player = playerInfo->id;
- item.window = window;
-
- mPlayerList->InsertItemsAt(1, 0, &item);
-
- }
-
- NSpPlayer_ReleaseEnumeration(theGame, thePlayers);
- }
- else
- cout << "NSpPlayer_GetEnumeration returned " << err << endl;
-
- bRunning = true;
- }
-
- void NSTestApp::HandleGamePause(NSpMessageHeader *inEvent)
- {
- cout << "In HandleGamePause" << endl;
- }
-
- void NSTestApp::HandleGameEnd(NSpMessageHeader *inEvent)
- {
- cout << "In HandleGameEnd" << endl;
- PlayerListItem item;
- LArrayIterator iter(*mPlayerList);
-
- while(iter.Next(&item))
- {
- item.window->DoClose();
- }
-
- mPlayerList->RemoveItemsAt(mPlayerList->GetCount(), LArray::index_First);
-
- bRunning = false;
- }
-
- void NSTestApp::HandleGameTerminated(NSpGameTerminatedMessage *inMessage)
- {
- gGameOver = true;
- }
-
- void NSTestApp::HandleTestGroups(NSpMessageHeader *inEvent)
- {
- cout << "\t!!Received a message to group " << inEvent->to << endl;
- }
-
- void NSTestApp::HandleResyncMessage(TKeyState *inEvent)
- {
- cout << "Received a resync message" << endl;
- }
-
- void NSTestApp::HandleKeyboardState(TKeyState *inEvent)
- {
- PlayerListItem info;
- ArrayIndexT index;
- UnsignedWide elapsedTime;
-
- index = mPlayerList->FetchIndexOfKey(&inEvent->h.from);
- #if DEBUG
- if (index == LArray::index_Bad)
- cout << "Got a bad player list index in HandleKeyboardState" << endl;
- #endif
- if (mPlayerList->FetchItemAt(index, &info))
- {
- CPlayerWindow *theWindow = (CPlayerWindow *)info.window;
-
- if (theWindow)
- {
- theWindow->mUp = inEvent->keyMap[0];
- theWindow->mDown = inEvent->keyMap[1];
- theWindow->mLeft = inEvent->keyMap[2];
- theWindow->mRight = inEvent->keyMap[3];
- theWindow->mPulse = inEvent->pulseBit;
- theWindow->mTransitTime = inEvent->h.when;
- theWindow->Refresh();
- theWindow->UpdatePort();
- }
- }
- }
-
- void NSTestApp::HandlePropogateOptions(OptionsMessage *inMessage)
- {
- gMessagePriority = inMessage->priority;
- gSendRate = inMessage->rate;
- gSendSize = inMessage->size;
-
- LCommander::SetUpdateCommandStatus(true);
-
- cout << "Received Options Propogation message from player " << inMessage->header.from << endl;
- }
-
- // ---------------------------------------------------------------------------
- // • ObeyCommand
- // ---------------------------------------------------------------------------
- // Respond to commands
-
- NSpProtocolListReference theList;
-
- Boolean
- NSTestApp::ObeyCommand(
- CommandT inCommand,
- void *ioParam)
- {
- Boolean cmdHandled = true;
-
- switch (inCommand) {
- case cmd_Host:
- DoHost();
- break;
- case cmd_Join:
- DoJoin();
- break;
- case cmd_GetInfo:
- DoGetInfo();
- break;
- case cmd_GetPlayers:
- DoGetPlayers();
- break;
- case cmd_GetGroups:
- DoGetGroups();
- break;
- case cmd_TestGroups:
- DoTestGroups();
- break;
- case cmd_Unadvertise:
- DoUnadvertise();
- break;
- case cmd_Start:
- DoStartGame();
- break;
- case cmd_Stop:
- DoGameEnd();
- break;
- case cmd_Quit:
- DoKillGame();
- LApplication::ObeyCommand(inCommand, ioParam);
- break;
- case 3001:
- gMessagePriority = 1;
- break;
- case 3002:
- gMessagePriority = 2;
- break;
- case 3003:
- gMessagePriority = 3;
- break;
- case 3004:
- gSendRate = 1;
- break;
- case 3005:
- gSendRate = 2;
- break;
- case 3006:
- gSendRate = 3;
- break;
- case 3007:
- gSendSize = 1;
- break;
- case 3008:
- gSendSize = 2;
- break;
- case 3009:
- gSendSize = 3;
- break;
- case 3011:
- DoResetOptions();
- case 3010:
- DoPropogateOptions();
- break;
- case 3012:
- DoSendResyncMessage();
- break;
- case 3013:
- gBlocking = !gBlocking;
- break;
- case 3014:
- if (gCallWNE)
- cout << "Hit F12 to resume calling WaitNextEvent" << endl;
- gCallWNE = !gCallWNE;
- break;
- default:
- cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
- break;
- }
-
- return cmdHandled;
- }
-
- // ---------------------------------------------------------------------------
- // • FindCommandStatus
- // ---------------------------------------------------------------------------
- // This function enables menu commands.
- //
-
- void
- NSTestApp::FindCommandStatus(
- CommandT inCommand,
- Boolean &outEnabled,
- Boolean &outUsesMark,
- Char16 &outMark,
- Str255 outName)
- {
-
- switch (inCommand) {
-
- // Return menu item status according to command messages.
- // Any that you don't handle will be passed to LApplication
-
- case cmd_Start:
- if (!bRunning)
- outEnabled = true;
- break;
- case cmd_Stop:
- if (bRunning)
- outEnabled = true;
- break;
- case cmd_Host:
- case cmd_Join:
- case cmd_GetInfo:
- case cmd_Unadvertise:
- case cmd_TestGroups:
- case cmd_GetPlayers:
- case cmd_GetGroups:
- case 3010:
- case 3011:
- case 3012:
- outEnabled = true;
- break;
- case 3001:
- outEnabled = true;
- outUsesMark = true;
- if (gMessagePriority == 1)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- case 3002:
- outEnabled = true;
- outUsesMark = true;
- if (gMessagePriority == 2)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- case 3003:
- outEnabled = true;
- outUsesMark = true;
- if (gMessagePriority == 3)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- case 3004:
- outEnabled = true;
- outUsesMark = true;
- if (gSendRate == 1)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- case 3005:
- outEnabled = true;
- outUsesMark = true;
- if (gSendRate == 2)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- case 3006:
- outEnabled = true;
- outUsesMark = true;
- if (gSendRate == 3)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- case 3007:
- outEnabled = true;
- outUsesMark = true;
- if (gSendSize == 1)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- case 3008:
- outEnabled = true;
- outUsesMark = true;
- if (gSendSize == 2)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- case 3009:
- outEnabled = true;
- outUsesMark = true;
- if (gSendSize == 3)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- case 3013:
- outEnabled = true;
- outUsesMark = true;
- if (gBlocking)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- case 3014:
- outEnabled = true;
- outUsesMark = true;
- if (gCallWNE)
- outMark = checkMark;
- else
- outMark = noMark;
- break;
- default:
- LApplication::FindCommandStatus(inCommand, outEnabled,
- outUsesMark, outMark, outName);
- break;
- }
- }
-
-
-
-
-
-
-
- void NSTestApp::DoHost(void)
- {
-
- OSStatus err;
- Boolean result;
- Str31 gameName;
- Str31 password;
- Str31 name;
-
- LString::CopyPStr("\pTest1", gameName);
- LString::CopyPStr("\pbarney", password);
- LString::CopyPStr("\pDarmok", name);
-
- NSpProtocolListReference theList = NULL;
-
- Try_
- {
- /*
- NSpProtocolReference atRef = NSpProtocol_CreateAppleTalk(gameName, kNBPNameString, 0, 0);
- cout << "NSpProtocol_CreateAppleTalk returned (non-zero) " << atRef << endl;
- NSpProtocolReference ipRef = NSpProtocol_CreateIP(2000, 0,0);
- cout << "NSpProtocol_CreateIP returned (non-zero) " << ipRef << endl;
- */
- // err = NSpProtocolList_New(atRef, &theList);
- err = NSpProtocolList_New(NULL, &theList);
- cout << "NSpProtocolListRef_New returned (0) " << err << endl;
- if (err != noErr)
- {
- // NSpProtocol_Dispose(atRef);
- // atRef = NULL;
- }
- ThrowIfOSErr_(err);
-
- /*
- err = NSpProtocolList_Append(theList, ipRef);
- cout << "NSpProtocolList_Append returned (0) " << err << endl;
- if (err != noErr)
- {
- NSpProtocol_Dispose(ipRef);
- atRef = NULL;
- }
- */
- ThrowIfOSErr_(err);
-
- result = NSpDoModalHostDialog(theList, gameName, name, password, NULL);
- if (!result)
- {
- NSpProtocolList_Dispose(theList);
- theList = NULL;
- return;
- }
-
- // If there is no name, the user doesn't want to play on this machine
- gServerOnly = (name[0] == 0) ? true : false;
-
- err = NSpGame_Host(&theGame, theList, 12, gameName, password, name, 0, kNSpClientServer, 0);
- cout << "NSpGame_Host returned (0) " << err << endl;
- ThrowIfOSErr_(err);
-
- NSpProtocolList_Dispose(theList);
-
- cout << "Our NetSprocket player ID is " << NSpPlayer_GetMyID(theGame) << endl;
-
- bAdvertising = true;
- bHost = true;
- }
- Catch_(code)
- {
- cout << "Exeption thrown in Host: " << code << endl;
- if (theList)
- NSpProtocolList_Dispose(theList);
- }
- }
-
-
- void NSTestApp::DoJoin(void)
- {
- OSStatus err = noErr;
- NSpAddressReference theAddress = NULL;
- Str31 name;
- Str31 password;
- LString::CopyPStr("\pJilad", name);
- LString::CopyPStr("\pbarney", password);
-
- theAddress = NSpDoModalJoinDialog(kNBPNameString, "\pGame servers:", name, password, NULL);
- cout << "DoModalJoinWithName returned an address ref of (non-zero) " << theAddress << endl;
- if (theAddress == NULL)
- return;
-
- err = NSpGame_Join(&theGame, theAddress, name, password, 0, (void*) kJoinRequestString, sizeof(kJoinRequestString),0);
- cout << "NSpGame_Join returned (0) " << err << endl;
-
- if (err == noErr)
- cout << "Our NetSprocket player ID is " << NSpPlayer_GetMyID(theGame) << endl;
-
- NSpReleaseAddressReference(theAddress);
- }
-
- void NSTestApp::DoGetInfo(void)
- {
- OSStatus status;
- NSpGameInfo info;
-
- status = NSpGame_GetInfo(theGame, &info);
- if (status != noErr)
- cout << "NSpGame_GetInfo returned an error: " << status << endl;
- else
- {
- cout << "Max Players: " << info.maxPlayers << endl;
- cout << "Current Players: " << info.currentPlayers << endl;
- cout << "Current Groups: " << info.currentGroups << endl;
- cout << "Topology: " << info.topology << endl;
- cout << "Game Name: " << p2cstr(info.name) << endl;
- cout << "Game Password: " << p2cstr(info.password) << endl;
- }
- }
- void NSTestApp::DoGetPlayers(void)
- {
- OSStatus status;
- NSpPlayerEnumerationPtr players;
- Str31 name;
-
- status = NSpPlayer_GetEnumeration(theGame, &players);
- if (status == kNSpNoPlayersErr)
- {
- cout << "There are no players in the game." << endl;
- return;
- }
- else
- cout << "NSpPlayer_GetEnumeration returned (0) " << status << endl;
-
- if (status != noErr)
- return;
-
- cout << "There are " << players->count << " players in the game:" << endl;
- for (int i = 0; i < players->count; i++)
- {
- LString::CopyPStr(players->playerInfo[i]->name, name);
- p2cstr(name);
- cout << "Player " << players->playerInfo[i]->id << ":" << endl;
- cout << "\tName: " << (char *)name << endl;
- cout << "\tType: " << players->playerInfo[i]->type << endl;
- cout << "\tGroup Count: " << players->playerInfo[i]->groupCount << endl;
-
- if (players->playerInfo[i]->groupCount > 0)
- {
- cout << "\tGroups: ";
- for (int j = 0; j < players->playerInfo[i]->groupCount; j++)
- {
- cout << players->playerInfo[i]->groups[j] << " ";
- }
- }
- cout << endl;
- }
-
- NSpPlayer_ReleaseEnumeration(theGame, players);
- }
-
- void NSTestApp::DoGetGroups(void)
- {
- OSStatus status;
- NSpGroupEnumerationPtr groups;
-
- status = NSpGroup_GetEnumeration(theGame, &groups);
- if (status == kNSpNoGroupsErr)
- {
- cout << "There are no groups in the game." << endl;
- return;
- }
- else
- cout << "NSpGroup_GetEnumeration returned (0) " << status << endl;
-
-
- cout << "There are " << groups->count << " groups in the game:" << endl;
- for (int i = 0; i < groups->count; i++)
- {
- cout << "Group " << groups->groups[i]->id << ":" << endl;
- cout << "\tPlayer Count: " << groups->groups[i]->playerCount << endl;
- if (groups->groups[i]->playerCount > 0)
- {
- cout << "Players: ";
- for (int j = 0; j < groups->groups[i]->playerCount; j++)
- {
- cout << groups->groups[i]->players[j] << " ";
- }
- cout << endl;
- }
- }
- }
-
- void NSTestApp::DoTestGroups(void)
- {
- NSpGroupID theGroup;
- NSpMessageHeader theMessage;
- OSStatus err;
-
- NSpClearMessageHeader(&theMessage);
- theMessage.what = kTestGroups;
- theMessage.messageLen = sizeof(NSpMessageHeader);
-
- err = NSpGroup_New(theGame, &theGroup);
- cout << "NSpGroup_New returned: " << err << endl;
- if (!err)
- cout << "\tCreated group number " << theGroup << endl;
-
- theMessage.to = theGroup;
-
- err = NSpGroup_AddPlayer(theGame, theGroup, 1);
- cout << "NSpGroup_AddPlayer returned " << err << endl;
-
- err = NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered);
-
- err = NSpGroup_AddPlayer(theGame, theGroup, 2);
- cout << "NSpGroup_AddPlayer returned " << err << endl;
-
- err = NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered);
-
- err = NSpGroup_RemovePlayer(theGame, theGroup, 1);
- cout << "NSpGroup_RemovePlayer returned " << err << endl;
-
- err = NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered);
-
- err = NSpGroup_AddPlayer(theGame, theGroup, 1);
- cout << "NSpGroup_AddPlayer returned " << err << endl;
-
- #if 0
- err = NSpGroup_Dispose(theGame, theGroup);
- cout << "NSpGroup_Dispose returned: " << err << endl;
- if (!err)
- cout << "\tDeleted group number " << theGroup << endl;
-
- err = NSpGroup_AddPlayer(theGame, theGroup, 2);
- cout << "NSpGroup_AddPlayer returned (should be err)" << err << endl;
- #endif
- }
-
-
- void NSTestApp::DoUnadvertise(void)
- {
- }
-
- void NSTestApp::DoStartGame(void)
- {
- NSpMessageHeader theMessage;
- OSStatus status;
-
- NSpClearMessageHeader(&theMessage);
- theMessage.what = kGameStart;
- theMessage.to = kNSpAllPlayers;
- theMessage.messageLen = sizeof(NSpMessageHeader);
-
- status = NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered | kNSpSendFlag_SelfSend);
- cout << "NSpMessage_Send returned (0) :" << status << endl;
- }
-
- void NSTestApp::DoGameEnd(void)
- {
- NSpMessageHeader theMessage;
- OSStatus status;
-
- if (theGame && bRunning)
- {
- NSpClearMessageHeader(&theMessage);
- theMessage.what = kGameEnd;
- theMessage.to = kNSpAllPlayers;
- theMessage.messageLen = sizeof(NSpMessageHeader);
-
- status = NSpMessage_Send(theGame, &theMessage, kNSpSendFlag_Registered | kNSpSendFlag_SelfSend);
- cout << "NSpMessage_Send returned (0) :" << status << endl;
- }
- }
-
- void NSTestApp::DoKillGame(void)
- {
- OSStatus status;
-
- if (!theGame)
- return;
- if (bHost)
- DoGameEnd();
-
- status = NSpGame_Dispose(theGame, kNSpGameFlag_ForceTerminateGame);
- cout << "NSpGame_Dispose returned (0) :" << status << endl;
- theGame = NULL;
-
- gGameOver = true;
- }
-
- void NSTestApp::DoResetOptions(void)
- {
- gMessagePriority = 1;
- gSendRate = 1;
- gSendSize = 1;
- }
-
- void NSTestApp::DoPropogateOptions(void)
- {
- OptionsMessage theMessage;
- OSStatus status;
-
- if (theGame)
- {
- NSpClearMessageHeader(&theMessage.header);
- theMessage.header.what = kPropogateOptions;
- theMessage.header.to = kNSpAllPlayers;
- theMessage.header.messageLen = sizeof(OptionsMessage);
- theMessage.priority = gMessagePriority;
- theMessage.rate = gSendRate;
- theMessage.size = gSendSize;
-
- status = NSpMessage_Send(theGame, &theMessage.header, kNSpSendFlag_Registered);
- }
- }
-
- void NSTestApp::DoSendResyncMessage()
- {
- NSpMessageHeader *theMessage;
- OSStatus status;
- theMessage = (NSpMessageHeader *) tenKMessage;
-
- NSpClearMessageHeader(theMessage);
- theMessage->what = kResyncMessage;
- theMessage->to = kNSpAllPlayers;
- theMessage->messageLen = 10 * 1024;
-
- ((TKeyState *)theMessage)->keyMap[0] = IsPressed(0x7e) ? 1 : 0;
- ((TKeyState *)theMessage)->keyMap[1] = IsPressed(0x7d) ? 1 : 0;
- ((TKeyState *)theMessage)->keyMap[2] = IsPressed(0x7c) ? 1 : 0;
- ((TKeyState *)theMessage)->keyMap[3] = IsPressed(0x7b) ? 1 : 0;
- ((TKeyState *)theMessage)->pulseBit = gPulseBit;
- gPulseBit = (gPulseBit == 1) ? 0 : 1;
-
- status = NSpMessage_Send(theGame, theMessage, kNSpSendFlag_SelfSend | kNSpSendFlag_Registered | kNSpSendFlag_FailIfPipeFull);
- if (status != noErr)
- cout << "NSpMessage_Send returned: " << status << endl;
- }
- Int32
- CPlayerListComparator::Compare(
- const void* inItemOne,
- const void* inItemTwo,
- Uint32 /* inSizeOne */,
- Uint32 /* inSizeTwo */) const
- {
- return ( (((PlayerListItem *)inItemOne)->player) - (((PlayerListItem *)inItemTwo)->player) );
- }
-
-
- Boolean
- CPlayerListComparator::IsEqualTo(
- const void* inItemOne,
- const void* inItemTwo,
- Uint32 /* inSizeOne */,
- Uint32 /* inSizeTwo */) const
- {
- return ( (((PlayerListItem *)inItemOne)->player) == (((PlayerListItem *)inItemTwo)->player) );
- }
-
-
- Int32
- CPlayerListComparator::CompareToKey(
- const void* inItem ,
- Uint32 /* inSize */,
- const void* inKey ) const
- {
- return ( (((PlayerListItem *)inItem)->player) - (*(NSpPlayerID *)inKey) );
- }
-
-
- Boolean
- CPlayerListComparator::IsEqualToKey(
- const void* inItem,
- Uint32 inSize,
- const void* inKey) const
- {
- return ( (((PlayerListItem *)inItem)->player) == (*(NSpPlayerID *)inKey) );
- }
-